
//
//  XLComposeToolBar.swift
//  SinaWeibo
//
//  Created by 李凯 on 16/9/11.
//  Copyright © 2016年 kevin. All rights reserved.
//

import UIKit

//按钮的枚举
enum composeToolBarBtnType: Int {
    case picture = 0
    case mention = 1
    case trend = 2
    case emoticon = 3
    case add = 4
}

//定义协议
protocol XLComposeToolBarDelegate: NSObjectProtocol {
    //定义代理方法
    func composeToolBar(toolBar: XLComposeToolBar, type: composeToolBarBtnType)
}

class XLComposeToolBar: UIView {
    
    //设置代理属性
    weak var delegate: XLComposeToolBarDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        
        addSubview(stackView)
        
        addChildButton(imgName: "compose_toolbar_picture", type: .picture)
        addChildButton(imgName: "compose_mentionbutton_background", type: .mention)
        addChildButton(imgName: "compose_trendbutton_background", type: .trend)
        addChildButton(imgName: "compose_emoticonbutton_background", type: .emoticon)
        addChildButton(imgName: "compose_add_background", type: .add)
        
        //布局
        stackView.snp_makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        
    }
    
    func addChildButton(imgName: String, type: composeToolBarBtnType) {
        let btn = UIButton()
        btn.setImage(UIImage(named: imgName), for: .normal)
        btn.setImage(UIImage(named:"\(imgName)_highlighted"), for: .highlighted)
        //记录按钮的类型
        btn.tag = type.rawValue
        //给btn添加点击事件
        btn.addTarget(self, action: #selector(btnClick(btn:)), for: UIControlEvents.touchUpInside)
        stackView.addArrangedSubview(btn)
    }
    
    //点击事件
    func btnClick(btn: UIButton){
        delegate?.composeToolBar(toolBar: self, type: composeToolBarBtnType(rawValue: btn.tag)!)
    }
    
    
    //懒加载
    lazy var stackView: UIStackView = {
        let stackView = UIStackView()
        stackView.distribution = .fillEqually
        return stackView
    }()

}
